<template>
  <div class="goods">
    <!-- 广告 -->
    <div class="banner">
      <img src="http://yanxuan.nosdn.127.net/8976116db321744084774643a933c5ce.png" alt="">
    </div>
    <!-- 导航 -->
    <div class="nav">
      <div :class="current == 1?'red':''" @click="navEvents(1)">综合</div>
      <div class="price" @click="navEvents(2)">
        <div :class="current == 2?'red':''">价格</div>
        <div class="arrow">
          <van-icon name="arrow-up" :class="orderCurrent == 'up'?'red':''" />
          <van-icon name="arrow-down" :class="orderCurrent == 'down'?'red':''" />
        </div>
      </div>
      <div :class="current == 3?'red':''" @click="navEvents(3)">分类</div>
    </div>
    <!-- 列表 -->
    <div class="list">
      <router-link tag="div" :to="`/home/goods/detail?id=${item.id}`" class="item" v-for='item in list' :key='item.id'>
        <img :src="item.list_pic_url" alt="">
        <div class="name">{{item.name}}</div>
        <div class="price">￥{{item.retail_price}}</div>
      </router-link>
    </div>
  </div>
</template>

<script>
import { goodsList } from "@/api/home/goods";
export default {
  data() {
    return {
      list: [], // 商品列表
      current: 1, // 1=综合 2=价格 3=分类
      orderCurrent: "", // up=上 down=下
      order: "", // 排序 asc 价格从小到展示  desc 价格从大到小展示
    };
  },
  // 计算
  computed: {},
  // 创建
  created() {
    this.init();
  },
  // 准备
  mounted() {},
  // 方法
  methods: {
    // 获取数据
    init() {
      var obj = {
        isHot: "", // 人气推荐
        isNew: "", // 新品首发
        order: this.order, // 排序
      };
      // 判断传值的是 新品首发1 还是热门推荐2
      if (this.$route.query.id == 1) {
        obj.isNew = 1;
      } else {
        obj.isHot = 1;
      }
      // 首页新品首发与人气推荐
      goodsList(obj).then((res) => {
        this.list = res.data.data;
      });
    },
    // 导航
    navEvents(val) {
      this.current = val;
      // 判断是否是2=价格
      if (this.current == 2) {
        if (this.orderCurrent == "up") {
          this.orderCurrent = "down";
          this.order = "asc";
        } else {
          this.orderCurrent = "up";
          this.order = "desc";
        }
      } else {
        this.orderCurrent = "";
        this.order = "";
      }
      this.init();
    },
  },
};
</script>

<style lang='scss' scoped>
.goods {
  // 广告
  .banner {
    img {
      width: 100%;
    }
  }
  // 导航
  .nav {
    height: 39px;
    display: flex;
    align-items: center;
    justify-content: space-around;
    background-color: #fff;
    .price {
      display: flex;
      align-items: center;
      .arrow {
        display: flex;
        flex-direction: column;
      }
    }
    .red {
      color: #b4282d;
    }
  }
  // 列表
  .list {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    padding-top: 10px;
    background-color: #f4f4f4;
    .item {
      width: calc(50% - 1px);
      background-color: #fff;
      padding: 7.5px 0;
      margin-bottom: 2.5px;
      img {
        width: 150px;
        height: 150px;
      }
      .name {
        padding: 0 10px;
        margin: 7.5px 0 11px 0;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
      }
      .price {
        font-size: 16px;
        color: #b4282d;
        margin: 15px auto;
      }
    }
  }
}
</style>